package schedule;

import business.XDateUtil;
import com.intellij.openapi.progress.ProgressManager;
import context.Context;
import leeks.FundBean;
import leeks.StockBean;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import service.AlertService;

import java.time.LocalDateTime;
import java.util.Set;

/**
 * 功能描述:
 *
 * @Author: luobo
 * @Date: 2021/2/8 0008 14:52:59
 */
public class DialogJob implements Job{

    private static final Context context = Context.getInstance();

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException{
        System.out.println("进入定时方法" + LocalDateTime.now());
        StringBuilder stringBuilder = new StringBuilder();

        Set<StockBean> stockBeanSet = context.getStockBeanSet();
        stockBeanSet.stream()
                .filter(stockBean -> {
                    String changePercent = stockBean.getChangePercent();
                    return Math.abs(Float.parseFloat(changePercent)) > 0.3;
                })
                .forEach(stockBean ->
                        stringBuilder.
                                append(stockBean.getName()).
                                append(" ,  ").
                                append(stockBean.getChangePercent()).
                                append("%").
                                append("<br>"));

        //展示内容:

        Set<FundBean> fundBeanSet = context.getFundBeanSet();
        fundBeanSet.stream()
                .filter(stockBean -> {
                    String changePercent = stockBean.getGszzl();
                    return Math.abs(Float.parseFloat(changePercent)) > 0.3;
                })
                .forEach(stockBean ->
                        stringBuilder.
                                append(stockBean.getFundName()).
                                append(" ,  ").
                                append(stockBean.getGszzl()).
                                append("%").
                                append("<br>"));
        stringBuilder.append("估值变动大 请留意!");

        ProgressManager.getInstance().executeNonCancelableSection(
                () -> {
                    XDateUtil.updateStockShowDate();
                    AlertService.getInstance().showAlertDialog(null, stringBuilder.toString());
                });
        System.out.println("该加仓了");
    }
}
